package com.example.demo.service.impl;

import com.example.demo.dao.ProjectDao;
import com.example.demo.entity.response.Response;
import com.example.demo.entity.sqlbean.Administrator;
import com.example.demo.entity.sqlbean.Project;
import com.example.demo.entity.sqlbean.ProjectDetails;

import com.example.demo.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectDao projectDao;

    /**
     * 查询项目列表信息
     * @param customerId
     * @return
     */
    @Override
    public Response selectProjectList(int customerId) {
        Response response = new Response();
        List<Project> projectList = projectDao.selectProjectById(customerId);
        if (projectList.isEmpty()) {

            response = Response.error(2001,"无信息");
            return response;
        }
        else
        {
            projectList.removeIf(project -> "0".equals(project.getIsOk()));
            for (int i = 0; i < projectList.size(); i++) {
                if (projectList.get(i).getCanSee()==0){

                    projectList.get(i).setEngineerId(0);

                }
            }
            response = Response.success(projectList);
        }


        return response;
    }

    /**
     * 查询单个项目详情
     * @param projectId
     * @return
     */
    @Override
    public Response selectProjectDetails(Long projectId) {
        Response response = new Response();
        ProjectDetails projectMessage = projectDao.selectProjectMessages(projectId);
        if (projectMessage == null) {
            response = Response.error(2002,"信息查询错误");
        }
        else
        {
            if (projectMessage.getCanSee()==0){
                projectMessage.setCompanyName(null);
            }
            ProjectDetails admin = projectDao.selectProjectAdmin(projectId);
            assert admin == null:"网络出错";
            projectMessage.setAdminImg(admin.getAdminImg());
            projectMessage.setAdminName(admin.getAdminName());


            response = Response.success(projectMessage);
        }

        return response;
    }
}
